package wqh.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import wqh.domain.*;
import wqh.pojo.IPConver;
import wqh.pojo.MapRet;
import wqh.pojo.MonitorAlarm;
import wqh.service.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by devts on 2016/9/28.
 */
public class AlarmConver {

    @Autowired
    private AlarmInCommingService alarmInCommingService;

    @Autowired
    private GeoBuildingService geoBuildingService;

    @Autowired
    private MonitorService monitorService;

    @Autowired
    private SensorService sensorService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private AlarmNoneInforService alarmNoneInforService;

    public AlarmConver()
    {

    }

    public Object getAllStatus(String status)
    {
        List<MonitorAlarm> monitorAlarmList = new ArrayList<MonitorAlarm>();
        try {
            List<AlarmInComming> alarmInComming = alarmInCommingService.findAllStatus(status);
            if(alarmInComming.size() == 0)  return MapRet.ALARM_DATAS(null);
            for(AlarmInComming a : alarmInComming)
            {
                String code = String.valueOf(a.getDev_addr());
                Device serverDevice = deviceService.findAllByIp(a.getIp());
                if(serverDevice == null) {
                    AlarmNoneInfor alarmNoneInfor = new AlarmNoneInfor(IPConver.int2Ip(a.getIp()),
                            a.getDev_type(),
                            a.getDev_addr(),
                            a.getDev_status(),
                            a.getAlarm_time(),
                            a.getConfirm_status());
                    alarmInCommingService.deleteRecord(a.getId());
                    alarmNoneInforService.insertRecord(alarmNoneInfor);
                    continue;
                }
                List<Monitor> monitorList = monitorService.findAllByDeviceAndCode(serverDevice.getId(),code);
                if(monitorList.size() == 0)
                {
                    AlarmNoneInfor alarmNoneInfor = new AlarmNoneInfor(IPConver.int2Ip(a.getIp()),
                            a.getDev_type(),
                            a.getDev_addr(),
                            a.getDev_status(),
                            a.getAlarm_time(),
                            a.getConfirm_status());
                    alarmInCommingService.deleteRecord(a.getId());
                    alarmNoneInforService.insertRecord(alarmNoneInfor);
                    continue;
                }
                Monitor monitor = monitorList.get(0);
                GeoBuilding geoBuilding1 = geoBuildingService.findRecordById(monitor.getGeo_level_1_id());
                GeoBuilding geoBuilding2 = geoBuildingService.findRecordById(monitor.getGeo_level_2_id());
                GeoBuilding geoBuilding3 = geoBuildingService.findRecordById(monitor.getGeo_level_3_id());
                Device device = deviceService.findAllByID(monitor.getDevice_id());
                Sensor sensor = sensorService.findAllByID(monitor.getSensor_id());
                monitorAlarmList.add(new MonitorAlarm(monitor,geoBuilding1,geoBuilding2,geoBuilding3,device,sensor,a));
            }
        }catch (Exception e)
        {
            System.out.println("报警信息转换异常。");
            e.printStackTrace();
        }
        return MapRet.ALARM_DATAS(monitorAlarmList);
    }

    public Object getAllStatusWeb(String status)
    {
        List<MonitorAlarm> monitorAlarmList = new ArrayList<MonitorAlarm>();
        try {
            //推送半小时内的
            Integer unix_time = new Long(System.currentTimeMillis() / 1000).intValue() - 1800;
            List<AlarmInComming> alarmInComming = alarmInCommingService.findAllStatusAndTime(status, unix_time);
            if(alarmInComming.size() == 0)  return MapRet.ALARM_DATAS(null);
            for(AlarmInComming a : alarmInComming)
            {
                String code = String.valueOf(a.getDev_addr());
                Device serverDevice = deviceService.findAllByIp(a.getIp());
                if(serverDevice == null) {
                    AlarmNoneInfor alarmNoneInfor = new AlarmNoneInfor(IPConver.int2Ip(a.getIp()),
                            a.getDev_type(),
                            a.getDev_addr(),
                            a.getDev_status(),
                            a.getAlarm_time(),
                            a.getConfirm_status());
                    alarmInCommingService.deleteRecord(a.getId());
                    alarmNoneInforService.insertRecord(alarmNoneInfor);
                    continue;
                }
                List<Monitor> monitorList = monitorService.findAllByDeviceAndCode(serverDevice.getId(),code);
                if(monitorList.size() == 0)
                {
                    AlarmNoneInfor alarmNoneInfor = new AlarmNoneInfor(IPConver.int2Ip(a.getIp()),
                            a.getDev_type(),
                            a.getDev_addr(),
                            a.getDev_status(),
                            a.getAlarm_time(),
                            a.getConfirm_status());
                    alarmInCommingService.deleteRecord(a.getId());
                    alarmNoneInforService.insertRecord(alarmNoneInfor);
                    continue;
                }
                Monitor monitor = monitorList.get(0);
                GeoBuilding geoBuilding1 = geoBuildingService.findRecordById(monitor.getGeo_level_1_id());
                GeoBuilding geoBuilding2 = geoBuildingService.findRecordById(monitor.getGeo_level_2_id());
                GeoBuilding geoBuilding3 = geoBuildingService.findRecordById(monitor.getGeo_level_3_id());
                Device device = deviceService.findAllByID(monitor.getDevice_id());
                Sensor sensor = sensorService.findAllByID(monitor.getSensor_id());
                monitorAlarmList.add(new MonitorAlarm(monitor,geoBuilding1,geoBuilding2,geoBuilding3,device,sensor,a));
            }
        }catch (Exception e)
        {
            System.out.println("报警信息转换异常。");
            e.printStackTrace();
        }
        return MapRet.ALARM_DATAS(monitorAlarmList);
    }

}
